﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data.SqlClient;

public partial class product_saving_admin_deposits : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal amount = decimal.Parse(tbAmount.Text);
        string strType = "WITHDRAW";
        string strFrom = tbFrom.Text;
        string strStatus = "SUCCESS";

        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnectionString);
       

        try
        {
        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandd = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);
        myConnection.Open();
        myCommandd.Parameters.AddWithValue("@date", DateTime.Now);
        myCommandd.Parameters.AddWithValue("@type", strType);
        myCommandd.Parameters.AddWithValue("@status", strStatus);
        myCommandd.Parameters.AddWithValue("@accNo", tbFrom.Text);
        myCommandd.Parameters.AddWithValue("@transactionAmount", 0-amount);
        myCommandd.ExecuteNonQuery();

        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandm = new SqlCommand("update [SAVING_ACCOUNT] SET [balance] = balance-" + amount + " WHERE [accountNo] = " + tbFrom.Text, myConnection);
        myConnection.Open();
        myCommandm.Parameters.AddWithValue("@balance", amount);
        myCommandm.Parameters.AddWithValue("@account", tbFrom.Text);
        myCommandm.ExecuteNonQuery();

        Label1.Text = "Withdrawal Success, please log in to check for the updated balance. Thank you.";

        myConnection.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
        Response.Write(ex);
        }
        finally
        {
        myConnection.Close();
        }
    }
}